package TimeWakeUp;

import sun.font.TrueTypeFont;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 薛剑
 * @date 2020-11-23 13:40
 */

/*
*
*
*
* 需求
* A线程输出5次，B线程输出10次，C线程输出15次，重复10次*/

class  MyShareTestData{

    private   int num=1;
    Lock lock = new ReentrantLock();

    private Condition A1 = lock.newCondition();
    private Condition B1 = lock.newCondition();
    private Condition C1 = lock.newCondition();

    public  void testshuchuA1()
    {
        try {
            lock.lock();
            while (num!=1) {
                A1.await();
            }
            for (int i = 1; i <=5 ; i++) {
                System.out.println("当前线程"+Thread.currentThread().getName()+"输出"+i);

            }
            num=2;
            B1.signal();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
//B1线程输出10次
    public  void testshuchuB1()
    {
        try {
            lock.lock();
            while (num!=2) {
               B1.await();
            }
            for (int i = 1; i <=10 ; i++) {
                System.out.println("当前线程"+Thread.currentThread().getName()+"输出"+i);
            }
            num=3;
            C1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
//C1线程输出15次
public  void testshuchuC1()
{
    try {
        lock.lock();
        while (num != 3) {
            C1.await();
        }

        for (int i = 1; i <=15 ; i++) {
            System.out.println("当前线程"+Thread.currentThread().getName()+"输出"+i);



        }
        num=1;
        A1.signal();
        A1.signal();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }

}
}
public class TestTimeUp {
    public static void main(String[] args) {
        MyShareTestData myShareTestData = new MyShareTestData();

        new Thread(()->{
            myShareTestData.testshuchuA1();
            System.out.println("Terst");
        },"A1").start();
        //
        new Thread(()->{
            myShareTestData.testshuchuB1();
        },"B1").start();
        //
        new Thread(()->{
            myShareTestData.testshuchuC1();
        },"C1").start();

    }
}
